package com.milk.controller.admin;

import com.milk.dto.CommonPageQueryDTO;
import com.milk.entity.Category;
import com.milk.result.PageResult;
import com.milk.result.Result;
import com.milk.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("AdminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@CrossOrigin
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;


    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping
    public Result<PageResult> pageQuery(CommonPageQueryDTO dto){
        log.info("分页查询信息");
        PageResult pageResult = categoryService.pageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Category category){
        log.info("新增分类信息");
        categoryService.insertCategory(category);
        return Result.success();
    }

    /**
     * 删除分类
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Long> ids){
        log.info("删除的Id = {}",ids);
        categoryService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Category category){
        log.info("修改分类的信息 = {}",category);
        categoryService.updateCategory(category);
        return Result.success();
    }
}
